<?php
/*
 * @Date: 2021-05-06 21:25:22
 * @Author: yukun
 * @LastEditors: yukun
 * @LastEditTime: 2021-05-06 21:56:45
 * @FilePath: /design-patterns-study/工厂模式/FactoryAbstract.php
 * @Description: 抽象工厂模式
 */

interface db
{
    public function connect();
}

interface Factory
{
    public function getDB();
}

class mysql implements db
{
    public function connect()
    {
        echo "已经连接了MySQL" . PHP_EOL;
    }
}

class sqlite implements db
{
    public function connect()
    {
        echo "已经连接了sqlite" . PHP_EOL;
    }
}

class mysqlFactory implements Factory
{
    public function getDB()
    {
        return new mysql();
    }
}

class sqliteFactory implements Factory
{
    public function getDB()
    {
        return new sqlite();
    }
}


// 抽象工厂方法
// 当需要增加一种新的数据库类型，只需要实现数据库类和对应的工厂类

$db = new mysqlFactory();
$db->getDB()->connect();


$db = new sqliteFactory();
$db->getDB()->connect();
